package com.jwsoft.manager.core.util;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.module.base.common.util.EncryptUtil;
import com.bifang.module.base.common.vo.common.CommonVO;
import com.bifang.module.base.core.integration.CommonIntegration;
import com.jwsoft.manager.common.vo.common.recruitStudent.RecruitStudentSyncReq;
import com.jwsoft.manager.common.vo.common.recruitStudent.RecruitStudentSyncRetVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.SocketException;
import java.net.SocketTimeoutException;


/**
 * 省教育专库同步工具类
 */
@Component
public class EduSyncZjUtil {
    @Autowired
    private CommonIntegration commonIntegration;

    public RecruitStudentSyncRetVO send(String url, String body,String areaCode) {
        if (StringUtils.isBlank(url)) {
            throw new AppException("请求地址不能为空");
        }
        if (StringUtils.isBlank(body)) {
            throw new AppException("请求内容不能为空");
        }
        if (StringUtils.isBlank(areaCode)){
            throw new AppException("请传入地区编码");
        }
        //获取appKey
        CommonVO appKeyVO = new CommonVO();
        appKeyVO.setParamKey("edu_zj_sync_appKey");
        appKeyVO.setAreaCode(areaCode);
        String appKey = commonIntegration.getValueByKeyAndArea(appKeyVO);
        if (StringUtils.isBlank(appKey)) {
            throw new AppException("请先在系统中配置配置省报名库同步的edu_zj_sync_appKey");
        }
        //获取appSecret
        CommonVO appSecretVO = new CommonVO();
        appSecretVO.setParamKey("edu_zj_sync_appSecret");
        appSecretVO.setAreaCode(areaCode);
        String appSecret = commonIntegration.getValueByKeyAndArea(appSecretVO);
        if (StringUtils.isBlank(appSecret)) {
            throw new AppException("请先在系统中配置配置省报名库同步的edu_zj_sync_appSecret");
        }
        //获取rsa公钥
        CommonVO rsaVO = new CommonVO();
        rsaVO.setParamKey("edu_zj_sync_rsa");
        rsaVO.setAreaCode(areaCode);
        String publicKey = commonIntegration.getValueByKeyAndArea(rsaVO);
        if (StringUtils.isBlank(publicKey)) {
            throw new AppException("请先在系统中配置配置省报名库同步的edu_zj_sync_rsa");
        }
        String params = null;
        try {
            params = EncryptUtil.encryptByPublicKey(body, publicKey);
        } catch (Exception e) {
            throw new AppException("报文加密失败", e);
        }
        String sign = SecureUtil.md5(params + appSecret);
        RecruitStudentSyncReq recruitStudentSyncReq = new RecruitStudentSyncReq();
        recruitStudentSyncReq.setAppKey(appKey);
        recruitStudentSyncReq.setSign(sign);
        recruitStudentSyncReq.setParams(params);
        String ret = null;
        try {
            ret = HttpUtil.post(url, JSON.toJSONString(recruitStudentSyncReq), 6000);
        } catch (Exception e) {
            throw new AppException("调用省报名库接口失败：" + e.getMessage());
        }
        if (StringUtils.isBlank(ret)) {
            throw new AppException("调用省报名库接口失败：未返回数据");
        }
        RecruitStudentSyncRetVO retVO = JSON.parseObject(ret, RecruitStudentSyncRetVO.class);
        return retVO;
    }
}
